﻿Imports ADOEntityLoginLog
Imports System.Data

Partial Class Administrator_HisToryLogin
    Inherits Web.UI.BasePage
    Private Const strMenuCode As String = "M68"
     Dim logLoginEntity1 As New LoginLogEntities

    Dim dslogLogin As New DataSet
    Dim clsQuery As clsDatabase
    Dim clsDataTable As New clsLINQToDataTable
    Dim intBeforeItemIndex As Integer

    Dim clsMasterDropDown As clsMasterDropDown
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim clsFormatDate As clsDateFormat
    Dim convertDate As clsDateFormat

#Region " Even Page "


    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        lblLoginType.Text = Resources.resHistoryLogin.LoginType

        btnSearch.Text = Resources.resButton.btnSearch
        btnPrint.Text = Resources.resButton.btnPrint

        lblMsg.Text = Resources.resMessageAlert.alertEmptyGrid
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            chkIsLogin()
            If (Not IsPostBack) Then
                LoadDataToDropdown()
                CreateControlMP()
                setDefaultSearch()
                'setUserRightButton()
            Else
                dslogLogin = Session("dsTblMaster")
                gvLogLogin.DataSource = dslogLogin.Tables("sLogLogin")
                gvLogLogin.DataBind()
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("dsTblMaster") = dslogLogin
    End Sub

#End Region

#Region "SetForm"

    Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "ผู้ดูแลระบบ > ประวัติการใช้งาน > ประวัติการเข้าระบบ"
    End Sub

#End Region

#Region " Even Button "

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        IsDataBind()
        clearGvIndex()
    End Sub

#End Region

#Region "Even GridView"

    Protected Sub gvLogLogin_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvLogLogin.PageIndexChanging
        gvLogLogin.PageIndex = e.NewPageIndex
        clearGvIndex()
        DataBind()
    End Sub

    Protected Sub gvLogLogin_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvLogLogin.Sorting
        Dim dt = TryCast(dslogLogin.Tables("sLogLogin"), DataTable)

        If dt IsNot Nothing Then

            'Sort the data.
            dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
            gvLogLogin.DataSource = dslogLogin.Tables("sLogLogin")
            gvLogLogin.DataBind()
            clearGvIndex()
        End If
    End Sub

    Protected Sub gvLogLogin_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvLogLogin.RowDataBound
        convertDate = New clsDateFormat
        If (e.Row.RowType.ToString = "DataRow") Then
            If Not (e.Row.Cells(3).Text = Nothing) Then
                Dim dteDateLogin As Date = e.Row.Cells(3).Text
                Dim strDate As String = ""
                strDate = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, strDateIsChk:=dteDateLogin.ToShortDateString)
                strDate = strDate & " " & dteDateLogin.ToLongTimeString
                e.Row.Cells(3).Text = strDate

            End If

        End If

    End Sub

#End Region

#Region " Function"
    'Private Sub setUserRightButton()

    '    Dim col As New Collection
    '    col.Add(btnSearch, statusbtn.Query.ToString)
    '    setBtnToUserRight(col, strMenuCode, clsSessionMenuRight.getDsMenuRight)

    'End Sub

    Private Sub LoadDataToDropdown()
        Try
            clsSessionMenuRight = Session("ClsMenuRight")
            clsMasterDropDown = New clsMasterDropDown

            clsMasterDropDown.setUserRightToDdlSearch(ddlUserRight, clsSessionMenuRight.getRoleGroup)

            clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                             strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                             strRoleCode:=clsSessionMenuRight.getRoleGroup)

            clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, _
                                                       strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                                 strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                 strZoneCode:=clsSessionMenuRight.getZoneCode)
        Catch ex As Exception
            WriteError(ex, "LoadDataToDropdown", "Historylogin")
        End Try



    End Sub

    Public Sub setDefaultSearch()
        Try
            clsFormatDate = New clsDateFormat
            txtStartDate.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
            txtEndDate.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)

        Catch ex As Exception
            WriteError(ex, "setDefaultSearch", "Historylogin")
        End Try



    End Sub

    Private Sub clearGvIndex()
        gvLogLogin.SelectedIndex = -1
        '  btnDelete.Attributes.Clear()
    End Sub

    Private Sub IsDataBind()
        Try

      
        Dim clsFormatDate As New clsDateFormat
        clsSessionMenuRight = Session("ClsMenuRight")

        If dslogLogin.Tables.Contains("sLogLogin") Then
            dslogLogin.Tables.Clear()
        End If
        'logLoginEntity1

        Dim qryLogLogin = From LogLogin In logLoginEntity1.SLOGINLOG Join _
                          dep In logLoginEntity1.SDEPARTMENT On LogLogin.DEPTCODE Equals dep.DEPTCODE _
                          Join branch In logLoginEntity1.SBRANCH On LogLogin.BRANCHCODE Equals branch.BRANCHCODE
                            Order By LogLogin.LOGINDATE Descending
                            Select LogLogin.USERNAME, LogLogin.EMPNAME, LogLogin.EMPLASTNAME, LogLogin.ROLECODE, LogLogin.DEPTCODE, dep.DEPTNAME, _
                          LogLogin.ZONECODE, LogLogin.BRANCHCODE, branch.BRANCHNAME, LogLogin.LASTIP, LogLogin.USERRIGHT, LogLogin.LOGINDATE,
                          LogintypeName = If(LogLogin.LOGINTYPE = 0, "ออกจากระบบ", If(LogLogin.LOGINTYPE = 1, "เข้าใช้ระบบ", "เข้าใช้งานไม่สำเร็จ")), LogLogin.LOGINTYPE

            'Dim qryLogLogin = From LogLogin In logLoginEntity.SLOGINLOG
            '                  Order By LogLogin.LOGINDATE Descending
            '                  Select LogLogin.USERNAME, LogLogin.EMPNAME, LogLogin.EMPLASTNAME, LogLogin.ROLECODE, LogLogin.DEPTCODE, _
            '                  LogLogin.ZONECODE, LogLogin.BRANCHCODE, LogLogin.LASTIP, LogLogin.USERRIGHT, LogLogin.LOGINDATE,
            '                  LogintypeName = If(LogLogin.LOGINTYPE = 0, "ออกจากระบบ", If(LogLogin.LOGINTYPE = 1, "เข้าใช้ระบบ", "เข้าใช้งานไม่สำเร็จ")), LogLogin.LOGINTYPE

        If (clsSessionMenuRight.getRoleGroup > 89) Then
            qryLogLogin = qryLogLogin.Where(Function(fn) fn.ROLECODE <= clsSessionMenuRight.getRoleGroup)
        ElseIf (clsSessionMenuRight.getRoleGroup > 79) Then
            qryLogLogin = qryLogLogin.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
        ElseIf (clsSessionMenuRight.getRoleGroup > 69) Then
            qryLogLogin = qryLogLogin.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)

        ElseIf (clsSessionMenuRight.getRoleGroup > 59) Then
            qryLogLogin = qryLogLogin.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup And fn.ZONECODE = clsSessionMenuRight.getZoneCode)
        ElseIf (clsSessionMenuRight.getRoleGroup < 60) Then
            qryLogLogin = qryLogLogin.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
            End If


        If Not (txtUsername.Text = "") Then
            qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.USERNAME.Contains(txtUsername.Text))
            End If
        If Not (txtFirstName.Text = "") Then
            qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.EMPNAME.Contains(txtFirstName.Text))
            End If
        If Not (txtLastName.Text = "") Then
            qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.EMPLASTNAME.Contains(txtLastName.Text))
            End If

        If Not (ddlUserRight.SelectedValue = "0") Then
            qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.USERRIGHT = ddlUserRight.SelectedValue)
            End If

        If Not (ddlDepartment.SelectedValue = "0") Then
            qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.DEPTCODE = ddlDepartment.SelectedValue)
            End If
        If Not (ddlBranch.SelectedValue = "0") Then
            qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.BRANCHCODE = ddlBranch.SelectedValue)
            End If


            '--------------เช็ค การติ้ก  chechbox
        Dim iCount As Integer = 0

        If (chkIsLoginPass.Checked) Then
            iCount = iCount + 1

            End If
        If (chkLogout.Checked) Then
            iCount = iCount + 1
            End If
        If (chkLoginFalse.Checked) Then
            iCount = iCount + 1
            End If

        If Not (iCount = 0) Then

            If (iCount = 1) Then
                qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.LOGINTYPE = If(chkIsLoginPass.Checked, 1, If(chkLogout.Checked, 0, 2)))
                End If

            If (iCount = 2) Then
                qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.LOGINTYPE = If(chkIsLoginPass.Checked, 1, If(chkLogout.Checked, 0, 2)) Or LogLogin.LOGINTYPE = If(chkLoginFalse.Checked, 2, 0))
                End If

            If (iCount = 3) Then
                qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.LOGINTYPE = 1 Or LogLogin.LOGINTYPE = 0 Or LogLogin.LOGINTYPE = 2)
            End If
        Else
            qryLogLogin = qryLogLogin.Where(Function(LogLogin) LogLogin.LOGINTYPE = 10 Or LogLogin.LOGINTYPE = 10 Or LogLogin.LOGINTYPE = 10)
        End If

        If Not (txtStartDate.Text = "" And txtEndDate.Text = "") Then
            Dim strStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtStartDate.Text) & " 00:00:00"
            Dim strEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) & " 23:59:59"

            qryLogLogin = qryLogLogin.Where(Function(tbl) tbl.LOGINDATE >= strStartDate And tbl.LOGINDATE <= strEndDate)
        End If


        dslogLogin.Tables.Add(clsDataTable.LINQToDataTable(qryLogLogin, "sLogLogin").Copy)
        If (qryLogLogin.Count = 0) Then
            lblTotal.Text = ""
            lblMsg.Visible = True
        Else
            lblMsg.Visible = False
            lblTotal.Text = "จำนวนรายการทั้งหมด " + qryLogLogin.Count.ToString + " รายการ "
            End If


        gvLogLogin.DataSource = qryLogLogin.ToList 'dslogLogin.Tables("sLogLogin")
        gvLogLogin.DataBind()


        clsDataTable = Nothing

        Catch ex As Exception
            WriteError(ex, "IsDataBind", "Historylogin")

        End Try
    End Sub



    Private Function GetSortDirection(ByVal column As String) As String

        ' By default, set the sort direction to ascending.
        Dim sortDirection = "DESC"

        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)

        If sortExpression IsNot Nothing Then
            ' Check if the same column is being sorted.
            ' Otherwise, the default value can be returned.
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing _
                  AndAlso lastDirection = "DESC" Then

                    sortDirection = "ASC"

                End If
            End If
        End If

        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function

#End Region

    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        Try
            Dim clsDropDown As New clsMasterDropDown
            clsSessionMenuRight = Session("ClsMenuRight")
            If Not (ddlDepartment.SelectedValue = "0") Then
                clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
            Else
                ddlBranch.SelectedValue = "0"
            End If

            clsDropDown = Nothing

        Catch ex As Exception
            WriteError(ex, "ddlDepartment_SelectedIndexChanged", "Historylogin")
        End Try

    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim param As String = "{ 'rptType' : '8'," _
                              & "'deptCode' : '" & ddlDepartment.SelectedValue & "/" & ddlDepartment.SelectedItem.Text & "'," _
                              & "'branchCode' : '" & ddlBranch.SelectedValue & "/" & ddlBranch.SelectedItem.Text & "'," _
                              & "'userRight' : '" & ddlUserRight.SelectedValue & "/" & ddlUserRight.SelectedItem.Text & "'," _
                              & "'userName' : '" & txtUsername.Text & "'," _
                              & "'empName' : '" & txtFirstName.Text.Trim & "'," _
                              & "'empLastName' : '" & txtLastName.Text.Trim & "'," _
                              & "'startDate' : '" & txtStartDate.Text & "'," _
                              & "'sEndDate' : '" & txtEndDate.Text & "'," _
                              & "'isLoginPass' : '" & If(chkIsLoginPass.Checked = True, 1, -1) & "/" & chkIsLoginPass.Text & "'," _
                              & "'logOut' : '" & If(chkLogout.Checked = True, 0, -1) & "/" & chkLogout.Text & "'," _
                              & "'LoginFalse' : '" & If(chkLoginFalse.Checked = True, 2, -1) & "/" & chkLoginFalse.Text & "'}"

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmPrintRptMaster.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)
    End Sub
End Class
